<?php

/**
 * CodeRS - Atviras kodas Lietuvai
 * Copyright (C) 2007 CodeRS www.coders.lt info@coders.lt
 * MightMedia TVS
 * bendras.php - nurodome kokius veiksmus atlikti prieš puslapiui užsikraunant
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * 
 * Ši programa yra laisva. Jūs galite ją platinti ir/arba modifikuoti
 * remdamiesi Free Software Foundation paskelbtomis GNU Bendrosios
 * Viešosios licencijos sąlygomis: 2 licencijos versija, arba (savo
 * nuožiūra) bet kuria vėlesne versija.
 * 
 * Ši programa platinama su viltimi, kad ji bus naudinga, bet BE JOKIOS
 * GARANTIJOS; be jokios numanomos PERKAMUMO ar TINKAMUMO KONKRETIEMS
 * TIKSLAMS garantijos. Žiūrėkite GNU Bendrąją Viešąją licenciją norėdami
 * sužinoti smulkmenas.
 * 
 * Jūs turėjote kartu su šia programa gauti ir GNU Bendrosios Viešosios
 * licencijos kopija; jei ne - rašykite Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**/

if ($_SERVER['PHP_SELF'] == __FILE__) { header('location: ?'); exit(); }

?>

<h2>Failų naršyklė</h2>
<?php
if (!ADMIN) { header('location: ?home'); exit(); }

// draudžiami failai
$denny = "update.php|.htaccess|javascript.html|toolbar_reklama.php|config.php|localhost.php|mg2_settings.php|sms_reklama.php|_config-rating.php|.svn|sms_config.php"; $denny = explode("|", $denny);
$pic = ".gif|.jpg|.bmp|.bmp|.ico"; $pic = explode("|", $pic);

// mygtukai
$img_back="images/admin/back.gif";
$img_folder="images/admin/folder.gif";
$img_file="images/admin/file.gif";
$img_home="images/admin/home.gif";
$img_surce="images/icons/page_white_code_red.png";


if (isset($url['v'])) {
	if (!in_array(basename($url['v']), $denny) && is_file((isset($url['d'])?urldecode($url['d']):'').$url['v'])) {
		$h = "<table bgcolor=#EEEEEE><tr><td width=30 valign='top' style=''><code>";
		for ($i = 1; $i <= count(file((isset($url['d'])?urldecode($url['d']):'').$url['v'])); $i++) $h .= $i.".<br>";
		$h .= "</code></td><td>";
		$h .= highlight_file((isset($url['d'])?urldecode($url['d']):'').$url['v'], true);
		$h .= "</td></tr></table>";
		hide($url['v']." - failo turinys",$h);
		unset($h);
	}
	else { die(klaida("Draudziama!","Konfidenciali informacija!")); }
}

function s($arr, $str) {
	if (is_array($arr)) {
		$ilgis = strlen($str); $return = '';
		foreach ($arr as $key => $val) {
			$tmp = substr($val, 0, $ilgis);
			if ($str == $tmp) {
				$return .= $val."\n";
			}
		}
		return $return;
	}
	return false;
}

// grazina failo pletini
function strip_ext($name,$ext='') {
	$ext = substr($name, strlen($ext)-4, 4);
	if(strpos($ext,'.') === false) {
		return "    "; // grazinam biski tarpu kad rusiavimas butu ciki,
	}
	return $ext; // jei tai failas grazinam jo pletini
}

// grazina failus is nurodytos direktorijos ir sukisa i masyva
function getFiles($path) {
	global $denny;
	$files = array();
	$fileNames = array();
	$path = urldecode($path);
	$i = 0;
	if (is_dir($path) && !in_array(dirname($path), $denny)) {
		if ($dh = opendir($path)) {
			while (($file = readdir($dh)) !== false) {
				if (($file != ".") && ($file != "..") && !in_array(basename($file), $denny)) {
					$fullpath = $path . "/" . $file;
					$fkey = $file;
					while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
					$files[$fkey]['size'] = baitai(filesize($fullpath));
					$files[$fkey]['name'] = $file;
					$e = strip_ext($file); // $e failo pletinys - pvz: .gif
					$files[$fkey]['type'] = filetype($fullpath); // failo tipas, dir, file ir pan
					$k=$e.$file; // kad butu lengvau rusiuoti;
					$fileNames[$i++] = $k;
					clearstatcache();
				}
			}
			closedir($dh);
		}
		else die (klaida("KLAIDA","Negalime nuskaityti direktorijos:  $path"));
	}
	else die (klaida("KLAIDA","Tai ne direktorija:  $path"));
	sort($fileNames,SORT_STRING); // surusiuojam
	$sortedFiles = array();
	$i = 0;
	foreach($fileNames as $f) {
		$f = substr($f, 4, strlen($f)-4); //sutvarko failo pletinius
		if($files[$f]['name'] !='') $sortedFiles[$i++] = $files[$f];
	}
	return $sortedFiles;
}

// Navigacija
$startdir = "./";
if(isset($url['d'])) {
	$url['d'] = urldecode($url['d']);
	$prev = $url['d'];
	$folder = $url['d'];
}
else { $folder = $startdir; $prev='';}
// END navigacija

$files = getFiles($folder);
$return = '';
foreach ($files as $file) {

	if(!in_array($file['name'],$denny)) {	//Jeigu tai nera htaccess failas
		$image = $img_file;
		$extra = "<a href=\"".url('v,'.$file['name'].'')."\"><img src=\"$img_surce\" border=\"0\"/></a>";
		if($file['type']=='dir') {	//jeigu direktorija
			$image = $img_folder;
			$extra = "";
			$cmd='?id,46;a,8;d,'.$prev.$file['name'].'/';
		}
		else $cmd=$prev.$file['name'];
		if ($file['type'] == 'file') { 
			if (in_array(ext($file['name']),$pic)) $return .= "$extra <a href=\"$cmd\" title=\"header=[".$file['name']."] body=[<center><br/><img src='".$cmd."'><br/><br/></center>] cssbody=[sarasas] cssheader=[title] fade=[on]\"><img src=\"$image\" border=\"0\" /> ".$file['name']."</a> <br/>";
			else $return .= "$extra <a href=\"$cmd\"><img src=\"$image\" border=\"0\" /> ".$file['name']."</a> <br/>";
		}
		else { $return .= "$extra <a href=\"$cmd\" title=\"".$file['type'].", ".$file['sizetext']."\"><img src=\"$image\" border=\"0\" /> ".$file['name']."</a> <br/>"; }
	}
}
if (isset($url['d'])) {
	$folder = explode("/",$url['d']);
	$link = ''; $dir = '';
	foreach ($folder as $fname) {
		$dir .= $fname."/";
		$link .= " <a href=\"?id,46;a,8;d,$dir\"\">$fname</a>";
	}
}
lentele("<a href=\"?id,46;a,8\">Failai</a>: ".((!empty($link))?$link:'')."", $return);

?>